#include "DefenseCannon.h"
#include "Grenade.h"
#include "RSManager.h"
#include "Sprite.h"
#include "Box.h"
#include <time.h>
#include <list>
#include "MainGame.h"
#include "ShareComponents.h"

DefenseCannon::DefenseCannon(Box* box) : Object(box, ObjEnemy_STATIC)
{

}


DefenseCannon::~DefenseCannon(void)
{

}

void DefenseCannon::update()
{	
	this->delay += GAME_TIME;
	if(_hits <= 0)
	{
		_sprite = _explode;
		if(delay > 100)
		{
			delay = 0;
			_sprite->Next();
			if(_sprite->GetCurrentIndex() == 3)
				_isAlive = false;
		}
	}
	else
	{
		if (delay > 550)
		{
			delay = 0;		
			if(_sprite->GetCurrentIndex() == 1)
			{
				_v = 2 + rand() % 3;
				_boxBullet = new Box(_box->_position - D3DXVECTOR2(10, 4), D3DXVECTOR2(0, 0), D3DXVECTOR2(_v, _v));
				Grenade* grenade = new Grenade(_boxBullet, 50, 145);			
				gl_MainGame->addWorldObject(grenade);	
			}
			_sprite->Next();							
		}
	}
}

void DefenseCannon::render()
{	
	_sprite->Render(_box->_position);	
}

void DefenseCannon::shoot()
{

}

void DefenseCannon::init(int spriteIndex)
{	
	delay = 0;
	_hits = 16;
	_isAlive = true;
	_cannon = RSManager::getInstance()->getSprite(DEFENSE_CANNON);
	_explode = RSManager::getInstance()->getSprite(EXPLODE_CANNON);
	_sprite = _cannon;
	_explode = RSManager::getInstance()->getSprite(EXPLODE_CANNON);
	_sprite->Reset(spriteIndex);
	_box->_size = _sprite->getRenderSize();	
	delay = 0;
	_box->_size = _sprite->getRenderSize();
	_colBox->_position = _box->_position;
	_colBox->_size = _box->_size;
	_colBox->_v = _box->_v;
}

void DefenseCannon::setIndexOfSprite(int index)
{
	_sprite->Reset(index);
}

Box* DefenseCannon::getCollisionBox()
{	
	return _colBox;
}
